package com.bei.lingluan.slice;

import com.bei.lingluan.ResourceTable;
import com.bei.lingluan.generalDialog.GeneralButtonElement;
import com.bei.lingluan.generalDialog.GeneralDialogHelper;
import com.bei.lingluan.hwid.HuaweiAccountSDKProxy;
import com.bei.lingluan.utils.Constant;
import com.bei.lingluan.utils.PreferencesHelper;
import com.bei.lingluan.utils.ResourceHelper;
import com.huawei.hms.accountsdk.constant.AccountSdkConstant;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

/**
 * @author 北海
 * @version 1.0
 * @description initialize ability slice
 * @date 2022/2/11
 */
public class InitializationAbilitySlice extends AbilitySlice {
    private final HiLogLabel LABEL_LOG = new HiLogLabel(HiLog.LOG_APP, AccountSdkConstant.LOG_DOMAIN, "InitializationAbilitySlice");
    private PreferencesHelper mUserConfigPreferences;
    private HuaweiAccountSDKProxy mHuaweiAccountSDKProxy;
    private Checkbox mCheckbox;

    private boolean mAgree = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_initialization);

        mUserConfigPreferences = new PreferencesHelper(getContext(), Constant.Name_UserConfigurationPreferences);
        if (mUserConfigPreferences.getBoolean(Constant.AGREE_KEY)) {

            present(new com.bei.lingluan.slice.MainAbilitySlice(), new Intent());
        }

        mHuaweiAccountSDKProxy = new HuaweiAccountSDKProxy();
    }

    @Override
    public void onActive() {
        super.onActive();
        initHuaweiLoginButtons();
        agreeStateListener();
        initPrivacy();
        initUseAppButtons();

    }

    private void initPrivacy() {
        Text text = findComponentById(ResourceTable.Id_agreement_initialization);
        text.setClickedListener((component) -> {
            showAgreement();
        });
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initHuaweiLoginButtons() {
        // 初始化华为帐号登录按钮
        Button mLogin = findComponentById(ResourceTable.Id_login_initialization);
        mLogin.setClickedListener((Component c) -> mHuaweiAccountSDKProxy.signIn());
    }

    private void initUseAppButtons() {
        Button use = findComponentById(ResourceTable.Id_use_initialization);
        use.setClickedListener(component -> {
            if (mAgree) {
                present(new com.bei.lingluan.slice.MainAbilitySlice(), new Intent());
            } else {

                new GeneralDialogHelper.Builder(getContext())
                        .setTitle(ResourceHelper.getString(getContext(),
                                ResourceTable.String_agreement_request,
                                "Please read and agree to the following agreement",
                                null))
                        .addClickableContent(ResourceHelper.getString(getContext(),
                                        ResourceTable.String_privacy_agreement,
                                        "\"User privacy agreement\"",
                                        null),
                                dialog -> {
                                    showAgreement();
                                    dialog.remove();
                                })
                        .addButton(ResourceHelper.getString(getContext(),
                                        ResourceTable.String_Confirm, "Confirm", null),
                                GeneralButtonElement.TYPE_ACCENT, dialog -> {
                            mCheckbox.setChecked(true);
                            dialog.remove();
                            present(new com.bei.lingluan.slice.MainAbilitySlice(), new Intent());
                        })
                        .addButton(ResourceHelper.getString(getContext(),
                                        ResourceTable.String_Cancel, "Cancel", null),
                                GeneralButtonElement.TYPE_NORMAL, dialog -> dialog.remove())
                        .create().show();
            }
        });
    }

    private void showAgreement() {
        Intent intent = new Intent();
        intent.setParam("fileName", "RuleOfLuciole.html");
        present(new WebViewSlice(), intent);
        mCheckbox.setChecked(true);
    }

    private void agreeStateListener() {
        mCheckbox = findComponentById(ResourceTable.Id_checkbox_initialization);
        mCheckbox.setCheckedStateChangedListener((component, state) -> {
            if (state) {
                mUserConfigPreferences.putSync(Constant.AGREE_KEY, true);
            }
            mAgree = state;
        });
    }

}

